﻿///////////////////////////////////////////////////////
//
// XDesigner.ORM 
//
// autohr : yuans
//
// date: 2011-2-23
//
// email  : yyf9989@hotmail.com
// 
//
///////////////////////////////////////////////////////

using System;
using System.Text;

namespace XDesigner.ORM
{
    /// <summary>
    /// Attribute special information bind data table
    /// </summary>
    [System.AttributeUsage( 
        AttributeTargets.Class
        | AttributeTargets.Struct
        | AttributeTargets.Enum
        | AttributeTargets.Interface )]
    public class ORMTypeAttribute : Attribute 
    {
        /// <summary>
        /// Initialize instance
        /// </summary>
        public ORMTypeAttribute()
        {
        }

        /// <summary>
        /// Initalize instance
        /// </summary>
        /// <param name="tableName">data base table name</param>
        public ORMTypeAttribute(string tableName)
        {
            strTableName = tableName;
        }

        private string strTableName = null;
        /// <summary>
        /// Data base table name
        /// </summary>
        public string TableName
        {
            get
            {
                return strTableName;
            }
            set
            {
                strTableName = value;
            }
        }

        private ORMBooleanValue _Buffered = ORMBooleanValue.Default ;
        /// <summary>
        /// Instance of this type buffered.
        /// </summary>
        [System.ComponentModel.DefaultValue(ORMBooleanValue.Default)]
        public ORMBooleanValue Buffered
        {
            get { return _Buffered; }
            set { _Buffered = value; }
        }

        private ORMBooleanValue _LogReference = ORMBooleanValue.False;
        /// <summary>
        /// Log reference
        /// </summary>
        public ORMBooleanValue LogReference
        {
            get { return _LogReference; }
            set { _LogReference = value; }
        }

        public static string GetTableName(Type t)
        {
            if (t == null)
            {
                throw new ArgumentNullException("t");
            }
            ORMTypeAttribute attr = (ORMTypeAttribute)Attribute.GetCustomAttribute(
                t,
                typeof(ORMTypeAttribute),
                true);
            if (attr != null)
            {
                if (Util.HasContent(attr.TableName))
                {
                    return attr.TableName.Trim();
                }
            }
            return t.Name;
        }
    }
}
